<?php


namespace app\admin\controller;


use app\common\model\Group;
use app\common\service\ServiceAdmin;
use think\Db;
use think\Request;

class Admin extends Base
{
    public function __construct()
    {
        parent::__construct();
        $this->isLogin();
    }

    /**
     * @route('admin/adminlist')
     * 后台账号列表
     */
    public function adminList()
    {
        $groupModel = new Group();
        $adminModel = new \app\common\model\Admin();

        // 获取搜索条件
        $get['user_name'] = input('get.user_name');
        $get['group_id'] = input('get.group_id');
//        $get['not_ins'] = [9,10,11];
        // 后台账号数据
        $rs_data = $adminModel->get_list($get,'status,user_id desc','a.*,b.group_id,b.group_name', $this->limit);

        // 管理组列表
        $group_data = $groupModel->get_list();
        $tvar = array(
            "title" => "账号列表",
            "time"  => time(),
            "data"  => $rs_data,
            "group" => $group_data,
            "get"   => $get,
        );
        $page = $rs_data->render();
        $this->assign("data", $tvar);
        $this->assign('page', $page);
        return $this->fetch('admin/adminlist');
    }


    /**
     * @route('admin/boxeadmin')
     */
    public function BoxeAdmin(Request $request)
    {
        $groupModel = new Group();
        $adminModel = new \app\common\model\Admin();
        $adminServ = new ServiceAdmin();


        if(\request()->isAjax()){
			$param = $request->param();

			if(!$param['user_id']){
                $admin = $adminServ->getAdmin($param['user_name']);
                if($admin){
                    $this->success('账号已注册','/admin/adminlist');
                }
            }

			// 处理
			$rs = $adminServ->DealAdmin($param);
			if($rs === false){
				$this->error($adminServ->getError());
			}
			$this->success('操作成功','/admin/adminlist');
        }

        $user_id = $_GET['user_id'];
        $admin = null;
        // 管理员组
        $group = $groupModel->get_list(['group_not_ins' => [9,10,11]]);
        if($user_id != 0){
            $admin = $adminModel->getOne(['user_id' => $user_id],'user_name,real_name,phone,group_id,user_id,status,remark,email,spf_id,shop_id');
            if($admin['shop_id'] != 0){
                // 管理员组
                $group = $groupModel->get_list(['group_ins' => [9,10,11]]);
            }
        }

        $tvar = array(
            "title" => "添加账号",
            "time"  => time(),
            "group" => $group,
            "user_id"   => $user_id,
        );
        $this->assign("data", $tvar);
        $this->assign("admin", $admin);
        return $this->fetch('admin/boxeadmin');
    }

    protected function json($msg){
        $msg = mb_convert_encoding( $msg, 'UTF-8', 'UTF-8,GBK,GB2312,BIG5' );
        return $msg;
    }

    /**
     * @route('admin/deladmin')
     * 删除用户
     */
    public function DelAdmin()
    {
        $user_id = $_GET['id'];

        $rs = \app\common\model\Admin::destroy($user_id);
        if($rs === false){
            $this->error("删除失败");
        }
        $this->success('删除成功 ');
    }



}